//
//  PS_DaiKuanApplyViewController.m
//  FinanceSupermarket
//
//  Created by 丹丹 on 2018/12/13.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_DaiKuanApplyViewController.h"
#import "PS_DaiKuanListTableViewCell.h"
#import "PS_ChooseProductViewController.h"
#import "PS_HomeListModel.h"
@interface PS_DaiKuanApplyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView * listTableView;
@property (nonatomic, strong)NSMutableArray * listArray;
@end

@implementation PS_DaiKuanApplyViewController

- (NSMutableArray *)listArray{
    
    if (_listArray == nil) {
        
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (UITableView *)listTableView{
    
    if (_listTableView == nil) {
        
        _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight+15, SCREEN_WIDTH,SCREEN_HEIGHT)];
        _listTableView.dataSource = self;
        _listTableView.delegate = self;
        _listTableView.showsVerticalScrollIndicator= NO;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.scrollEnabled = NO;
    }
    
    return _listTableView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.nav_titleLabel.text = @"贷款申请";
    [self.view addSubview:self.listTableView];
    
    [self getProductList];
    
    // Do any additional setup after loading the view from its nib.
}

#pragma mark - delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.listArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 190;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PS_DaiKuanListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PS_DaiKuanListTableViewCell"];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"PS_DaiKuanListTableViewCell" owner:self options:nil]objectAtIndex:0];
        
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.applyButton addTarget:self action:@selector(submitButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.applyButton.enabled = NO;
    cell.applyButton.tag = indexPath.section + 50;
    
    PS_HomeListModel * model = [self.listArray objectAtIndex:indexPath.row];
    
//    float w = [self widthForString:model.name fontSize:17 andHeight:30];
    cell.titleLabel.text = model.name;
    cell.contentLabel.text = model.subtitle;
//    cell.width_TitleLabel.constant = w+10;
//    cell.contentTitleLabel.text = model.subtitle;
//    cell.sub_ContentLabel.text = model.description2;
//
    NSString * imageUrl = [NSString stringWithFormat:@"%@",model.image];
    [cell.backImage sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
    return cell;
    
}

-(void)submitButtonAction:(UIButton*)button{
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PS_HomeListModel * model = [self.listArray objectAtIndex:indexPath.row];
    PS_ChooseProductViewController * VC = [[PS_ChooseProductViewController alloc]init];
    VC.pId = [NSString stringWithFormat:@"%ld",(long)model.productId];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)getProductList
{
    [Utils post:GETPRODUCTLIST parameters:nil success:^(NSInteger recode, id jsonObject) {
        
        NSArray * arr = [jsonObject objectForKey:@"data"];
        for (NSDictionary * dic in arr) {
    
                PS_HomeListModel * model = [PS_HomeListModel mj_objectWithKeyValues:dic];
                model.productId = [[dic objectForKey:@"id"] integerValue];
                model.description2 = [dic objectForKey:@"description"];
                [self.listArray addObject:model];
    
            }
        
        [self.listTableView reloadData];
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

@end
